package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"strings"
	"sync"
)

/*
	将SPONSORED的排名算在一起
 */
func handleResult(result string, itemNumber string) (rankInfo map[int]map[string]int, totalNumber int) {
	itemNumberString := "listingId=\"" + itemNumber + "\""
	doc, _ := goquery.NewDocumentFromReader(strings.NewReader(result))
	itemMap := make(map[int]string)
	sele := doc.Find("li[listingId]")
	totalNumber = sele.Length()
	rankInfo = make(map[int]map[string]int)
	if !strings.Contains(result, itemNumberString) {
		return
	}
	doc.Find("li[listingId]").Each(func(i int, selection *goquery.Selection) {
		htmlStr, err := selection.Html()
		if err != nil {
			fmt.Println("selection.html Err:", err)
		}
		reg := regexp.MustCompile(`iid="(\d+)"`)
		res := reg.FindAllStringSubmatch(htmlStr, 1)
		itemMap[i] = res[0][1]
		//fmt.Println("开始判断是否找到排名")
		//fmt.Println(res[0][1],"-",itemNumber)
		singleItemInfo := make(map[string]int)
		x := 0
		if res[0][1] == itemNumber { //找到item 确认是否是 SPONSORED
			if strings.Contains(htmlStr, "<div class=\"promoted-lv\"><span>SPONSORED</span></div>") {
				singleItemInfo["currPageRank"] = i
				singleItemInfo["sponsored"] = 1
			} else {
				singleItemInfo["currPageRank"] = i
				singleItemInfo["sponsored"] = 0
			}
			rankInfo[x] = singleItemInfo
			x++
		}
		//fmt.Println("i:",i)
	})
	return
	//fmt.Println("len(itemMap):",len(itemMap))
	//fmt.Println("itemMap:",itemMap)
}
func spiderOneUrl(url, itemNumber string, i int, keyword string, ch chan int, ifCalcSponsored bool) {
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("http.Get Err:", err)
		return
	}
	defer resp.Body.Close()
	f, err1 := os.Create(keyword + "_" + strconv.Itoa(i) + ".html")
	if err1 != nil {
		fmt.Println("os.Create Err:", err1)
		return
	}

	buf := make([]byte, 1024*4)
	result := ""
	for {
		n, _ := resp.Body.Read(buf)
		result += string(buf[:n])
		if n == 0 {
			break
		}
	}
	f.WriteString(result)
	rankInfo, totalNumber := handleResult(result, itemNumber)
	if len(rankInfo[0]) != 0{
		fmt.Println(rankInfo)
		fmt.Println("page:",i)
		fmt.Println(totalNumber)
	}
	ch <- i
}
func doEveryKeywordWork(keyword string, maxPage, numberPerPage int, wg *sync.WaitGroup, itemNumber string, ifCalcSponsored bool) {
	ch := make(chan int)
	preUrl := "https://www.ebay.co.uk/sch/i.html?_from=R40&_fcid=3&_clu=2&_localstpos=LE3%208DX&_stpos=LE3%208DX&gbr=1"
	url := ""
	for i := 1; i <= maxPage; i++ {
		url = preUrl + "&_nkw=" + keyword + "&_pgn=" + strconv.Itoa(i) + "&_ipg=" + strconv.Itoa(numberPerPage)
		fmt.Printf("开始爬取关键词(%s)的第%d页:\n", keyword, i)
		go spiderOneUrl(url, itemNumber, i, keyword, ch, ifCalcSponsored)
	}
	for i := 1; i <= maxPage; i++ {
		fmt.Printf("关键词(%s)的第%d页爬取完毕:\n", keyword, <-ch)
	}
	defer wg.Done()
}
func main() {
	var itemNumber string
	itemNumber = "123355396438"
	//itemNumber = "123515889163"
	//itemNumber = "264035473380"
	var wg sync.WaitGroup
	maxPage := 20
	numberPerPage := 50
	ifCalcSponsored := true
	//keywords := []string{"bluetooth%20headphones"}
	//keywords := []string{"bluetooth%20headphones","true%20wireless%20bluetooth%20earphones"}
	keywords := []string{"true%20wireless%20bluetooth%20earphones"}
	//fmt.Println(keywords)
	wg.Add(len(keywords))
	for _, v := range keywords {
		go doEveryKeywordWork(v, maxPage, numberPerPage, &wg, itemNumber, ifCalcSponsored)
	}
	wg.Wait()
}
